package com.qb.devserv;

import static com.qb.devserv.FactoryActivity.fActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;

import org.json.JSONObject;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import receiver.NotificationReceiver;
import utils.DevInfoUtil;
import utils.MqttUtil;
import utils.MyUtil;
import utils.NotificationUtil;

public class MainService extends Service {
    Context ctx;
    public static MainService mService;
    public MqttUtil mu = new MqttUtil();
    public SharedPreferences SP;
    public MainService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ctx = this;
        mService = this;
        SP = getSharedPreferences("dev_mgt_setting", 0);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MyUtil.showLog("onStartCommand","onStartCommand");
        String board_factory = SP.getString("BOARD_FACTORY","");
//        if (board_factory.equals("XH") || board_factory.equals("zim")) {
        if (DevInfoUtil.getBrand().equals(ctx.getString(R.string.invalid_device_key))) {
            foreGroundNotifaction("启动于"+MyUtil.formatDateTime(new Date()));
            mu.init(ctx);
        } else {
            foreGroundNotifaction(ctx.getString(R.string.invalid_device_tip));
        }

        // 查询终端管理平台是否存在本设备
        //rollTask("dev_init",5);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            stopForeground(true);
        }
    }

    // mqtt发布指令
    public void sendMsg(String f, JSONObject d) {
        mu.sendMsg(f,d);
    }

    // startForeground
    public void foreGroundNotifaction(String msg) {
        NotificationUtil nu = new NotificationUtil(ctx);
//        NotificationManager manager = nu.getManager();
//        Intent intent = new Intent(ctx, MainActivity.class);
//        PendingIntent pi = PendingIntent.getActivity(ctx, 10086, intent, 0);
        Notification notification = nu.setContentIntent(null)
                .setFlags(Notification.FLAG_AUTO_CANCEL)
                .setOnlyAlertOnce(true)
                .setAutoCancel(false)
                .setOngoing(true)
                .getNotification("服务正在运行", msg, R.drawable.icon_baseline_desktop_mac_24);

        //manager.notify(10086, notification);
        startForeground(10086,notification);
    }

    // 轮询任务
    public Timer mTimer;
    public TimerTask mTimerTask;
    public Timer rollTask(String flag,int second){
        if (mTimer != null) mTimer.cancel();
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                MyUtil.showLog("rollTask",flag+"@"+new Date());
                if (flag.equals("factory_activity")) {
                    if (!MyUtil.isActivityForeground(ctx)) {
                        Intent activity = new Intent(ctx, FactoryActivity.class);
                        activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        ctx.startActivity(activity);
                    }
                }
            }
        };
        mTimer.schedule(mTimerTask,1 * 1000,second * 1000);
        MyUtil.showLog(flag, mTimer);
        return mTimer;
    }
    public void stopTask(Timer t) {
        MyUtil.showLog("stopTask", t);
        if (t != null) t.cancel();
    }
}